﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;
using System.Configuration;
using System.Web;

namespace Ewts
{
    public partial class MainFrm : System.Web.UI.MasterPage
    {
        AccountsPrincipal user;
        User currentUser;
        public string strWelcome;
      

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (!Context.User.Identity.IsAuthenticated)
                {
                    Response.Clear();
                    Response.Write("<script language=javascript>window.alert('您没有权限进入本页！\n请登录或与管理员联系！');history.back();</script>");
                    Response.Redirect("/accounts/login.aspx");
                    Response.End();
                }
                else
                {
                    if (Session["UserInfo"] == null)
                    {
                        Response.Write("<script language=javascript>window.alert('您没有权限进入本页！\n请登录或与管理员联系！');history.back();</script>");
                        Response.Redirect("/accounts/login.aspx");
                        return;
                    }
                    if (Request.Cookies["tally"] != null)
                    {
                        HttpCookie myCookie = new HttpCookie("tally");
                        myCookie.Expires = DateTime.Now.AddDays(-1d);
                        Response.Cookies.Add(myCookie);
                    }
                    currentUser = (DataLib.Accounts.Bus.User)Session["UserInfo"];
                    lblUser.InnerHtml = currentUser.TrueName;
                    menu.InnerHtml = GetStrhtml();
                    //this.lblSignIn.InnerText = currentUser.TrueName;
                    //blab.InnerHtml = "<a href='login.aspx'>重新登录</a>";
                }


            }


        }


        protected string GetStrhtml()
        {
            /**********原始HTML
        <ul>
            <li class="menu_1">
                <img src="images/sc.png" /><br />
                市场管理</li>
            <li class="menu_2">
                <ul>
                    <li class="menu_3">市场数据</li>
                    <li class="menu_4">
                        <ul>
                            <li class="menu_5"><a>客户列表</a></li>
                            <li class="menu_5">Load列表</li>
                            <li class="menu_5">危废录入</li>
                            <li class="menu_5">退运/直销</li>
                            <li class="menu_5">待授权表</li>
                            <li class="menu_5">单据确认</li>
                            <li class="menu_5">新建CJO</li>
                        </ul>
                    </li>
                </ul>
                <ul>
                    <li class="menu_3">市场报表</li>
                    <li class="menu_4">
                        <ul>
                            <li class="menu_5">LTR报表</li>
                        </ul>
                    </li>
                </ul>
                <ul>
                    <li class="menu_3">项目管理</li>
                    <li class="menu_4">
                        <ul>
                            <li class="menu_5">项目管理</li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
             *  <li class='menu_1'>
                <a id='urlback' onclick='backInit()'><img src='/images/back.png' /><br />
                Back</a></li>
            */
            //总的菜单模板
            string strtemplate0 = @"
                    {0}
            <ul class='ul0'>
            <li class='menu_10'>
                <a id='urlback'><img src='/images/back.png' /><br />
                返    回</a></li>
            </ul>";
            //1级菜单html模板
            string strtemplate1=@"<ul class='ul1'>
                <li class='menu_1'>
                <img src='{0}' /><br />
                {1}</li>
                {2}
                </ul>";

            //一级菜单补充
            string strtemplate1_0 = @"<li class='menu_2'>
                     {0}
             </li>";
            //2级菜单html模板
            string strtemplate2 = @"
                <ul>
                    <li class='menu_3'>{0}</li>
                    {1}
                </ul>
            ";
            //3级菜单html模板
            string strtemplate3 = @"<li class='menu_4'>
                        <ul>
                            {0}
                        </ul>
                    </li>";
            //内容模板
            string strtemplate4 = @" <li class='menu_5'>{0}</li>";
            //string strmenu = "<ul>";
            string strmenu0 = "";   //总菜单
            string strmenu1 = "";   //1级菜单
            string strmenu2 = "";   //2级菜单
            string strmenu3 = "";   //3级菜单
            string strmenu4 = "";   //菜单内容
            string url1 = "";
            string url2 = "";
            string url3 = "";
            string imgurl = "";
            user = new AccountsPrincipal(Context.User.Identity.Name);
 
            DataLib.BLL.SysTree sm = new DataLib.BLL.SysTree();
            //顶部一级目录赋值
            DataSet ds;
            ds = sm.GetTreeList("ParentID=0");

            int permissionid = 0;
            int permissionid2 = 0; //2级菜单权限
            int permissionid3 = 0;  //3级菜单权限
 


            //一级菜单
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                strmenu2 = "";
                imgurl = "";
                permissionid = int.Parse(r["PermissionID"].ToString().Trim());
              
                if ((permissionid == -1) || (user.HasPermissionID(permissionid)))
                {
                    url1 = r["TreeText"].ToString();
                    
                    DataTable dt = sm.GetTreeList("ParentID=" + r["NodeID"].ToString()).Tables[0];
                    foreach (DataRow row2 in dt.Rows)
                    {
                        strmenu3 = "";
                        strmenu4 = "";
                        permissionid2 = int.Parse(row2["PermissionID"].ToString().Trim());
                        if ((permissionid2 == -1) || (user.HasPermissionID(permissionid2)))
                        {
                            if (row2["URL"] != null && row2["URL"].ToString().Trim() != "")
                            {
                                url2 = "<a href='" + row2["URL"].ToString() + "'>" + row2["TreeText"].ToString() + " </a>";
                            }
                            else
                            {
                                url2 = row2["TreeText"].ToString();
                            }
 
                            DataTable dt3 = sm.GetTreeList("ParentID=" + row2["NodeID"].ToString()).Tables[0];
                            if (dt3.Rows.Count > 0)
                            {
                                foreach (DataRow row3 in dt3.Rows)
                                {
                                     permissionid3 = int.Parse(row3["PermissionID"].ToString().Trim());
                                    if ((permissionid3 == -1) || (user.HasPermissionID(permissionid3)))
                                    {
                                        //strmenu += "<li class='menu3'>";
                                        //strmenu += "<div class='div3'>";
                                        // strmenu += "<a ";
                                        if (row3["URL"] != null && row3["URL"].ToString().Trim() != "")
                                        {
                                            url3 = "<a href='" + row3["URL"].ToString() + "'>" + row3["TreeText"].ToString() + " </a>";
                                            //strmenu += "href='" + row3["URL"].ToString() + "'";
                                        }
                                        else
                                        {
                                            url3 = row3["URL"].ToString();
                                        }
                                         strmenu4 += String.Format(strtemplate4, url3);

                                        //strmenu += ">";
                                        //strmenu += row3["TreeText"].ToString();
                                        //strmenu += "</a>";
                                        //strmenu += "</div>";
                                        //strmenu += "</li>";
                                    }
                                }
                                strmenu3 += String.Format(strtemplate3, strmenu4);
                            }

                            strmenu2 += String.Format(strtemplate2, url2, strmenu3);
                            
                        }

                    }

                    if (dt.Rows.Count > 0)
                    {
                        strmenu2 = String.Format(strtemplate1_0, strmenu2);
                    }
                    // strmenu += "</ul>";
                    //一级菜单结束
                     //strmenu += "</li>";
                    
                    imgurl = r["ImageUrl"].ToString();
                    strmenu1 += String.Format(strtemplate1, imgurl, url1, strmenu2);
                }
            }
            //strmenu += "</ul>";
            //return strmenu;
            strmenu0 = String.Format(strtemplate0, strmenu1);
            return strmenu0;


        }

    }
}